package cn.iocoder.yudao.module.dbit.controller.admin.schinfo;

import cn.iocoder.yudao.module.dbit.controller.admin.majinfo.vo.MajInfoRespVO;
import cn.iocoder.yudao.module.dbit.dal.dataobject.majinfo.MajInfoDO;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.dbit.controller.admin.schinfo.vo.*;
import cn.iocoder.yudao.module.dbit.dal.dataobject.schinfo.SchInfoDO;
import cn.iocoder.yudao.module.dbit.service.schinfo.SchInfoService;

@Tag(name = "管理后台 - 共建学校")
@RestController
@RequestMapping("/dbit/sch-info")
@Validated
public class SchInfoController {

    @Resource
    private SchInfoService schInfoService;

    @PostMapping("/create")
    @Operation(summary = "创建共建学校")
    @PreAuthorize("@ss.hasPermission('dbit:sch-info:create')")
    public CommonResult<Integer> createSchInfo(@Valid @RequestBody SchInfoSaveReqVO createReqVO) {
        return success(schInfoService.createSchInfo(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新共建学校")
    @PreAuthorize("@ss.hasPermission('dbit:sch-info:update')")
    public CommonResult<Boolean> updateSchInfo(@Valid @RequestBody SchInfoSaveReqVO updateReqVO) {
        schInfoService.updateSchInfo(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除共建学校")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('dbit:sch-info:delete')")
    public CommonResult<Boolean> deleteSchInfo(@RequestParam("id") Integer id) {
        schInfoService.deleteSchInfo(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得共建学校")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<SchInfoRespVO> getSchInfo(@RequestParam("id") Integer id) {
        SchInfoDO schInfo = schInfoService.getSchInfo(id);
        return success(BeanUtils.toBean(schInfo, SchInfoRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得共建学校分页")
    @PreAuthorize("@ss.hasPermission('dbit:sch-info:query')")
    public CommonResult<PageResult<SchInfoRespVO>> getSchInfoPage(@Valid SchInfoPageReqVO pageReqVO) {
        PageResult<SchInfoDO> pageResult = schInfoService.getSchInfoPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, SchInfoRespVO.class));
    }

    @GetMapping("/list")
    @Operation(summary = "获得所有共建学校")
    public CommonResult<List<SchInfoRespVO>> getSchInfoList() {
        List<SchInfoDO> result = schInfoService.getSchInfoList();
        return success(BeanUtils.toBean(result, SchInfoRespVO.class));
    }

    @GetMapping("/getMajorList")
    @Operation(summary = "获得对应的专业列表")
    public CommonResult<List<MajInfoRespVO>> getMajorList(@RequestParam("schId") Integer schId) {
        List<MajInfoRespVO> result = schInfoService.getMajorList(schId);
        return success(result);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出共建学校 Excel")
    @PreAuthorize("@ss.hasPermission('dbit:sch-info:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportSchInfoExcel(@Valid SchInfoPageReqVO pageReqVO,
                                   HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<SchInfoDO> list = schInfoService.getSchInfoPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "共建学校.xls", "数据", SchInfoRespVO.class,
                BeanUtils.toBean(list, SchInfoRespVO.class));
    }

}